package com.chenu.gamscriptman.domain.noindb;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @author flyan
 * date 2019-12-25
 * @function 自定义分页Bean，继承自Mybatis Plus的分页对象，用于完成分页功能
 * 添加了新功能：动态显示条
 */
public class PageBean<T> extends Page<T> {

    /* 附加功能：动态显示条
     * 动态显示条期望值，默认值10；
     * 这个值只是期望，比如说，我们期望动态显示条展示10条分页数据，
     * 但是实际上只有7条，那么也只能显示7条有效数据咯
     */
    private long dynamicHopeSize = 10;
    private long dynamicStart = 1;
    private long dynamicEnd = 10;

    public PageBean() {
        super();
    }

    public PageBean(long current, long size, long dynamicHopeSize) {
        super(current, size);
        this.dynamicHopeSize = dynamicHopeSize;
    }

    public PageBean(long current, long size, long total, long dynamicHopeSize) {
        super(current, size, total);
        this.dynamicHopeSize = dynamicHopeSize;
    }

    public PageBean(long current, long size, boolean isSearchCount, long dynamicHopeSize) {
        super(current, size, isSearchCount);
        this.dynamicHopeSize = dynamicHopeSize;
    }

    public PageBean(long current, long size, long total, boolean isSearchCount, long dynamicHopeSize) {
        super(current, size, total, isSearchCount);
        this.dynamicHopeSize = dynamicHopeSize;
    }

    public void calcDynamicBar(){
        /* 动态显示条 */
        if(this.getPages() < this.dynamicHopeSize){
            /* 总页数小于期望值，那么范围就是 1 ~ 当前总页数 */
            this.dynamicStart = 1;
            this.dynamicEnd = this.getPages();
        } else {
            /* 先算出页界限：期望值 - 1 */
            int limit = (int)(this.dynamicHopeSize - 1);
            /* 总页数大于期望值
             * 让其动态范围控制在，期望值前40%后50%里。
             */
            int nrLeftPage = (int) (this.dynamicHopeSize * 0.4);    /* 前40% */
            this.dynamicStart = this.getCurrent() - nrLeftPage;
            int nrRightPage = limit - nrLeftPage;
            this.dynamicEnd = this.getCurrent() + nrRightPage;
            /* 如果当前动态开始值小于最小值，那么我们将它拉回来，不可能前往第0页 */
            if(this.dynamicStart < 1){
                this.dynamicStart = 1;
                this.dynamicEnd = this.dynamicHopeSize;
            }
            /* 如果当前动态结束值大于最大值，那么我们将它拉回来，不可能前往不存在的页面 */
            if(this.dynamicEnd > this.getPages()){
                this.dynamicEnd = this.getPages();
                this.dynamicStart = this.dynamicEnd - limit;
            }
        }
    }

    public long getDynamicHopeSize() {
        return dynamicHopeSize;
    }

    public void setDynamicHopeSize(long dynamicHopeSize) {
        this.dynamicHopeSize = dynamicHopeSize;
        calcDynamicBar();
    }

    public long getDynamicStart() {
        return dynamicStart;
    }

    public long getDynamicEnd() {
        return dynamicEnd;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "pages=" + this.getPages() +
                ", size=" + this.getSize() +
                ", current=" + this.getCurrent() +
                ", dynamicHopeSize=" + dynamicHopeSize +
                ", dynamicStart=" + dynamicStart +
                ", dynamicEnd=" + dynamicEnd +
                '}';
    }
}
